import json
import os
import sys
from colorama import init,Fore

init(autoreset=True)
def format_str_color( str ):
  str_list=str.split("  ")
  str_list_1=[]
  for str in str_list:
    if len(str.strip()) != 0:
      str_list_1.append(str.strip())
  if "loaded active running" in str_list_1:
    for str in str_list_1:
      if str == "loaded active running":
        print(Fore.GREEN + str + "    ", end="")
      else:
        print(str + "    ", end = "")

    print()

def get_results_stdout( all_the_text ):
  list = all_the_text.split("ok")
  for list_value in list:
    if len(list_value.strip()) == 0:
      continue;
    list2 = list_value.split("=>")
    print((list2[0].split(':'))[1].strip())
    data2 = json.loads(list2[1])
    load_dict = data2['result']
    results= load_dict['results']
    for result_list_value in results:
      result_dict=result_list_value
      format_str_color(result_dict['stdout'])

def get_result_stdout_lines ( all_the_text ):
  list = all_the_text.split("ok")
  for list_value in list:
    if len(list_value.strip()) == 0:
      continue;
    list2 = list_value.split("=>")
    print((list2[0].split(':'))[1].strip())
    data2 = json.loads(list2[1])
    load_dict = data2['result']
    #print(load_dict)
    data_list = load_dict['stdout_lines']
    for list_value in data_list:
      print(list_value)

  
str = open(sys.argv[1]).read()
if sys.argv[2] == "stdout":
  get_results_stdout( str )
else:
  get_result_stdout_lines( str )
